<?php
include('../function/config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title><?php echo $config['title'];?></title>
<link rel="shortcut icon" href="../image/Letter-T.ico" />
<style type="text/css">
body {
	background-color: #E6F2FF;
	margin: 20px auto;
	padding-top: 30px;
	height: 100%;
	width: 900px;
}

.content{
	background-color: #ffffff;
}
</style>
<script>
function setFocus(){
document.formedit.add_money.focus();
}

function CheckMoney(pay){
	if(document.formedit.add_money.value < pay){
		alert('กรุณาตรวจสอบเงินที่รับใหม่ค่ะ');
		document.formedit.add_money.focus();
		return false;
	}

	document.formedit.submit();
}

function cash(){
	var elm1 = parseInt(document.getElementById("total").value);
	var elm2 = document.getElementById("add_money").value;
	var result;
	var total;

	result = elm2-elm1;

	document.getElementById("receive_money").value = result;
	
}
</script>
</head>
<body onload="setFocus()">
<?php
include("../connect/connect.php");
include("../function/thaidate.php");

$Query = "SELECT debtor_total.*,bill.bill_no as bill_no FROM debtor_total,bill WHERE debtor_total.bill_id = bill.bill_id AND debtor_total_id = '".$_GET["id"]."'";
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$row = mysql_fetch_array($objQuery);


if($_GET["action"]=="update") {

	if($_POST['add_money'] != "")
		{
			
	$time = time();
	
	$d = date("H", $time);

	if($d<='5'){
		$ddate = strtotime("-1 day" , $time);
		$time3 = date("Y-m-d",  $ddate);
	} else {
		$time3 = date("Y-m-d",  $time);
	}
	
			//เปลี่ยนสถานะเป็น 1=ชำระเงินแล้ว
			$Query = mysql_query("UPDATE debtor_total SET debtor_total_status = '1' WHERE debtor_total_id = '".$_POST["debtor_id"]."' ");
			
			//เพิ่มข้อมูลการชำระเงินเพื่ออกรายงานประจำวัน
			$Query1 = mysql_query("INSERT INTO debtor_report (debtor_report_name,debtor_report_date,debtor_report_pay,debtor_report_bill,debtor_report_total) VALUES ('".$_POST['debtor_total_name']."','".$_POST['debtor_total_date3']."','".$time3."','".$_POST['bill']."','".$_POST['total']."')");
			$Query2 = mysql_query("INSERT INTO debtor_report_session (debtor_report_name,debtor_report_date,debtor_report_pay,debtor_report_bill,debtor_report_total) VALUES ('".$_POST['debtor_total_name']."','".$_POST['debtor_total_date3']."','".$time3."','".$_POST['bill']."','".$_POST['total']."')");
			
			header('Location: debtor_manage.php');
			exit();
		}
}
?>
<form action="?action=update" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>รับชำระหนี้</h1>
    <div class="buttons"><a onclick="JavaScript:if(confirm('คุณต้องการยืนยันการชำระเงิน ใช่หรือไม่?')==true){CheckMoney(<?php echo $row['debtor_total_total']; ?>);}" class="button"><span>ยืนยันการชำระเงิน</span></a><a onclick="location = 'debtor_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>ชื่อ : </td>
        <td><?php echo $row["debtor_total_name"];?><input name="debtor_total_name" type="hidden"  value="<?php echo $row["debtor_total_name"];?>"  /><input name="debtor_id" type="hidden"  value="<?php echo $_GET["id"];?>"  /></td>
      </tr>
      <tr>
        <td>เบอร์โทรศัพท์ : </td>
        <td><?php if($row["debtor_total_phone"]==""){ echo "-"; } else { echo $row["debtor_total_phone"]; }?></td>
      </tr>
      <tr>
        <td>วันที่ค้างชำระ : </td>
        <td><?php echo thai_date($row['debtor_total_date']); ?><input name="debtor_total_date3" type="hidden"  value="<?php echo $row["debtor_total_date3"];?>"  /><input name="bill" type="hidden"  value="<?php echo $row["bill_no"];?>"  /></td>
      </tr>
      <tr>
        <td>ยอดเงิน : </td>
        <td><input name="total" type="text" id="total" value="<?php echo $row['debtor_total_total']; ?>" readonly="readonly" style="text-align: right;"  /> บาท</td>
      </tr>
      <tr>
        <td>รับเงิน : </td>
        <td><input name="add_money" type="text" id="add_money" onkeyup="cash();" style="text-align: right;" /> บาท</td>
      </tr>
      <tr>
        <td>ทอนเงิน : </td>
        <td><input name="receive_money" type="text" id="receive_money" readonly="readonly" value="0" style="text-align: right;" /> บาท</td>
      </tr>
    </table>
  </div>
</div>
</form>
</body>
<?php mysql_close(); ?>
</html>
